<?php
/**
 * Created by PhpStorm.
 * User: zhangkai
 * Date: 2017/6/6
 * Time: 下午10:02
 */

namespace App\Http\Model\Admin;

use Illuminate\Database\Eloquent\Model;
use App\Http\Model\Admin\TouristAction;
use Kafka\Produce;


class PushData extends Model
{
    public function push()
    {
        $touristAction = new TouristAction();

        // -- 将数据向kafka推送 --
        for ($index = 0;$index < 100;$index++) {
            $touristAction->setTable('tourists_actions_'.$index);
            // 获取所有已完成清洗但尚未画像的数据
            $dataList = array_merge($dataList, $touristAction->getAllByDataStatus(2));
        }

        $validDataList = [];
        $invalidDataList = [];

        // 校验数据的完整性，判断是否可以送交画像
        foreach ($dataList as $data) {
            if ($this->checkDataValid($data)) {
                $validDataList[] = $data;
            }
            else {
                $invalidDataList[] = $data;
            }
        }

        // 将数据送交画像
        $this->pushDataToKafka($validDataList);


        // 修改数据状态
        foreach ($validDataList as $data) {
            $touristAction->updateById($data['phone'], $data['id'], array('data_status' => 3));
        }

        foreach ($invalidDataList as $data) {
            $touristAction->updateById($data['phone'], $data['id'], array('sdata_tatus' => 4));
        }

        // 将数据向API的接口推送
        // todo


        return $validDataList;

    }

    public function pushDataToKafka($dataList = [])
    {
        $topic = 'portraitOffline';
        $pushDataList = [];

        foreach ($dataList as $data) {
            $pushData = array(
                "house_info" => array(
                    "house_floor" => isset($data['house_floor']) ? intval($data['house_floor']) : 0,
                    "house_room" => $data['house_room'],
                    "cityarea_id" => $data['cityarea_id'],
                    "house_totalarea" => $data['house_totalarea'],
                    "house_price" => $data['house_price'],
                    "house_hall" => $data['house_hall'],
                    "cityarea2_id" => $data['cityarea2_id'],
                    "borough_id" => $data['borough_id'],
                    "house_topfloor" => $data['house_topfloor'],
                    "user" => array(
                        "uid" => md5($data['phone'] . 'zkzgzF'),
                        "appName"=>'offline',
                        "create_time" => time(),
                        "idfa" => $data['idfa'],
                        "imei" => $data['imei'],
                        "event" => 'taiyue',
                        )
                )
            );


            $pushDataList[] = json_encode($pushData);
        }
	
        $produce = Produce::getInstance("10.45.142.19:2181, 10.25.114.207:2181,10.25.115.91:2181", 50000);

        $produce->setRequireAck(-1);
        //$partition = $produce->getAvailablePartitions($topic);

        $produce->setMessages($topic, 0, $pushDataList);
        $result = $produce->send();

    }


    private function checkDataValid($data)
    {
        if ($data['borough_id'] == '') {
            return false;
        }

        return true;
    }


}
